<template>
  <div class="app-container">
  <el-row :gutter="20" class="header">
    <el-col :span="7">
      <el-input placeholder="请输入角色名..." v-model="queryForm.name"
        clearable></el-input>
    </el-col>
    <el-button type="primary" :icon="Search" @click="initRoleList">搜索</el-button>
    <el-button type="success" :icon="DocumentAdd"
      @click="handleDialogValue()">新增</el-button>
    <el-popconfirm title="您确定批量删除选用角色吗？" @confirm="handleDeleteBatch">
      <template #reference>
        <el-button type="danger" :disabled="delBtnStatus" :icon="Delete">批量删除</el-button>
      </template>
    </el-popconfirm>
  </el-row>
  <el-table
    :data="tableData"
    stripe
    style="width: 100%"
    @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55"/>
    <el-table-column prop="name" label="角色名" width="100" align="center"/>
    <el-table-column prop="code" label="权限字符" width="200" align="center"/>
    <el-table-column prop="create_time" label="创建时间" width="200"
      align="center"/>
    <el-table-column prop="update_time" label="更新时间" width="200"
      align="center"/>
    <el-table-column prop="remark" label="备注"/>
    <el-table-column prop="action" label="操作" width="400" fixed="right" align="center">
      <template v-slot="scope">
        <el-button type="primary" :icon="Tools"
          @click="handleMenuDialogValue(scope.row.id)">分配权限</el-button>
        <el-button v-if="scope.row.code!='admin'" type="primary" :icon="Edit"
          @click="handleDialogValue(scope.row.id)"></el-button>
        <el-popconfirm v-if="scope.row.code!='admin'" title="您确定要删除这条记录吗？" @confirm="handleDelete(scope.row.id)">
          <template #reference>
            <el-button type="danger" :icon="Delete"/>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
    v-model:currentPage="queryForm.page"
    v-model:page-size="queryForm.page_size"
    :page-sizes="[10, 20, 30, 40,50]"
    layout="total, sizes, prev, pager, next, jumper"
    :total="total"
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
  />
  <Dialog v-model="dialogVisible" :dialogVisible="dialogVisible" :id="id" :dialogTitle="dialogTitle" 
  @initRoleList="initRoleList"></Dialog>
  <MenuDialog v-model="menuDialogVisible" :menuDialogVisible="menuDialogVisible" :id="id"
  @initRoleList="initRoleList"></MenuDialog>
</div>
</template>

<script setup>
import { Search, Tools, RefreshRight, Edit, Delete, Plus, DocumentAdd, } from '@element-plus/icons-vue'
import {ref} from 'vue'
import requestUtil, {getServerUrl} from "@/util/request";
import {ElMessage, ElMessageBox} from 'element-plus'
import Dialog from './components/dialog.vue'
import MenuDialog from './components/menuDialog.vue'

const queryForm = ref({
  name: '',
  page: 1,
  page_size: 10
})
const total = ref(0)
const tableData = ref([])
const id = ref(-1)
const delBtnStatus = ref(true)
const dialogTitle = ref("")
const dialogVisible = ref(false)
const multipleSelection = ref([])
const menuDialogVisible = ref(false)

const initRoleList = async () => {
  try {
    const res = await requestUtil.get('system/role', queryForm.value)
    tableData.value = res.results;
    total.value = res.count;
  } catch (error) {
    console.error('获取角色列表失败', error)
    ElMessage.error('获取角色列表失败')
  }
}

initRoleList();

const handleSizeChange = (page_size) => {
  queryForm.value.page = 1;
  queryForm.value.page_size = page_size;
  initRoleList();
}
const handleCurrentChange = (page) => {
  queryForm.value.page = page;
  initRoleList();
}

const handleDialogValue = (roleId) => { 
  if (roleId) {
    id.value = roleId;
    dialogTitle.value = "角色编辑"
  }else {
    id.value = -1;
    dialogTitle.value = "角色添加"
  }
  dialogVisible.value = true
}

const handleDelete = (roleId) => {
  if (roleId) {
      requestUtil.del(`system/role/${roleId}/`)
      .then(res => {
        try { 
          ElMessage.success('删除成功')
          initRoleList()
        } catch (error) {
          ElMessage.error('删除失败')
        }
      })
    }
}

const handleSelectionChange = (val) => {
  console.log("勾选了")
  console.log(val)
  multipleSelection.value = val
  delBtnStatus.value = val.length == 0
}

const handleDeleteBatch = () => {
  if (multipleSelection.value.length > 0) {
    const roleIds = multipleSelection.value.map(item => item.id)
    requestUtil.del('system/role/batch_delete/', { ids: roleIds })
      .then(res => {
        ElMessage.success('删除成功')
        initRoleList()
      })
      .catch(() => {
        ElMessage.error('删除失败')
      })
  }
}

const handleMenuDialogValue = (roleId) => {
  if (roleId) {
    id.value = roleId;
  }
  menuDialogVisible.value = true
}

</script>
<style lang="scss" scoped>
.header {
padding-bottom: 16px;
box-sizing: border-box;
}
.el-pagination {
float: right;
padding: 20px;
box-sizing: border-box;
}
::v-deep th.el-table__cell {
word-break: break-word;
background-color: #f8f8f9 !important;
color: #515a6e;
height: 40px;
font-size: 13px;
}
.el-tag--small {
margin-left: 5px;
}
</style>
